import XMonad

import qualified XMonad.Actions.Search as S
import qualified XMonad.Actions.Submap as SM

import qualified Data.Map as M

import XMonad.Layout.NoBorders(smartBorders)

import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Hooks.ManageHelpers
import XMonad.Hooks.UrgencyHook

import XMonad.Prompt
import XMonad.Prompt.Shell

import XMonad.Util.Run(spawnPipe)
import XMonad.Util.EZConfig(additionalKeys)

import System.IO


main = do

    xmobH <- spawnPipe "xmobar"
    

    xmonad $ withUrgencyHook NoUrgencyHook $ defaultConfig
        { manageHook = manageDocks 
                       <+> composeAll [(isFullscreen --> doFullFloat)]
                       <+> manageHook defaultConfig
        , layoutHook = smartBorders . avoidStruts $ layoutHook defaultConfig
		, logHook = dynamicLogWithPP $ myXmobarPP xmobH
		, modMask = mod4Mask
		, focusFollowsMouse = True
		, terminal = "xterm"
		, workspaces = zipWith ((++) . show) [1..9] (map (wsLabelSep ++) wsLabels)
		, borderWidth = 2
		, normalBorderColor = myGrey25
		, focusedBorderColor = myGrey75
		} `additionalKeys`
		[ ((mod4Mask, xK_backslash), shellPrompt myXPConfig)
		, ((mod4Mask, xK_s ), SM.submap $ searchEngineMap $ S.promptSearch myXPConfig)
		, ((mod4Mask .|. shiftMask , xK_s ), SM.submap $ searchEngineMap $ S.selectSearch)
		, ((0, 0x1008FF13), spawn "amixer -q set Master 2dB+")
		, ((0, 0x1008FF11), spawn "amixer -q set Master 2dB-")
		, ((0, 0x1008FF12), spawn "amixer -q set Master toggle")
		]


--         Font
myFont = "DejaVu Sans Mono-9"
-- end of: Font


--         Workspaces
wsLabelSep = "."
wsLabels = [ "web"
           , "code"
           , "term"
           , "gen"
           , "doc"
           , "msg"
           , "vos"
           , "batch"
           , "hide"]
myCurW = ("| ", " |")
myVisW = ("  ", "  ")
myHidW = ("  ", "  ")
myEmpW = (" ", " ")
myUrgW = ("=", "=")

myLayW = (" # ", " # ")
-- end of: Workspaces


--         Colors
myACol = "#007A58"
myBCol = "#8BA314"
myCCol = "#66002B"
myDCol = "#161329"
myECol = "#A33600"

myBlack  = "#101010"
myWhite  = "#FFFFFF"
myGrey25 = "#3F3F3F"
myGrey50 = "#7F7F7F"
myGrey75 = "#BFBFBF"
-- end of: Colors


--         ColorScheme
myCurCols  = (myBlack, myBCol)
myVisCols  = (myBCol, myBlack)
myHidCols  = (myBCol, myBlack)
myEmpCols  = (myGrey50, myBlack)
myUrgCols  = (myBCol, myBlack)

myLayCols = (myBlack, myGrey50)
-- end of: ColorScheme


--         XP options
myXPConfig = defaultXPConfig
	{ font = "xft:" ++ myFont
	, fgColor = myGrey75
	, bgColor = myBlack
	, fgHLight = myBlack
	, bgHLight = myBCol
	, borderColor = myGrey50
	, position = Top
	}
-- end of: XP options


--         search engines
searchEngineMap method = M.fromList $
       [ ((0, xK_g), method S.google )
       , ((0, xK_c), method S.codesearch )
       , ((0, xK_t), method S.mathworld )
       , ((0, xK_y), method S.youtube )
       , ((0, xK_m), method S.maps )
       , ((0, xK_d), method S.dictionary )
       , ((0, xK_w), method S.wikipedia )
       , ((0, xK_b), method $ S.searchEngine "archbbs" "http://bbs.archlinux.org/search.php?action=search&keywords=")
       , ((0, xK_r), method $ S.searchEngine "AUR" "http://aur.archlinux.org/packages.php?O=0&L=0&C=0&K=")
       , ((0, xK_a), method $ S.searchEngine "archwiki" "http://wiki.archlinux.org/index.php/Special:Search?search=")
       ]
-- end of: search engines

--         xmobar PP
myXmobarPP h = defaultPP
    { ppCurrent         = uncurry xmobarColor myCurCols . uncurry wrap myCurW 
    , ppVisible         = uncurry xmobarColor myVisCols . uncurry wrap myVisW
    , ppHidden          = uncurry xmobarColor myHidCols . uncurry wrap myHidW
    , ppHiddenNoWindows = uncurry xmobarColor myEmpCols . uncurry wrap myEmpW
    , ppUrgent          = uncurry xmobarColor myUrgCols . uncurry wrap myUrgW
    , ppSep = "  "
    , ppWsSep = " "
    , ppTitle = take 0 
    , ppLayout = uncurry xmobarColor myLayCols . uncurry wrap myLayW
    , ppOutput = hPutStrLn h
    }
-- end of: xmobar PP

